<?php

namespace Api\Controller;

use Api\Controller\CommonController;
use Think\Log;

class UserController extends CommonController
{
    /*登陆*/
    public function login()
    {
        $_validate = array(
            array('mobile', '手机号必须填写'),
            array('password', '登陆密码必须填写'),
            array('userId', '初始化错误'),
            array('channelId', '初始化错误')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $car_db = D('Car');
        $car = $car_db->login($data['mobile'], $data['password']);
        if ($car === FALSE) {
            $this->errorReturn("登陆出错，账号或密码错误");
        } else {
            $car_db->where(array('id' => $car['id']))->save(array('user_id'=>$data['userId'],'channel_id'=>$data['channelId']));
            $this->successReturn('操作成功', $car);
        }
    }
    
    public function loginout(){
        $_validate = array(
            array('carId', '手机号必须填写')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $car = D('Car');
        $res = $car->where(array('id' => $data['carId']))->save(array('online'=>0));
        if ($res) {
            $this->successReturn('操作成功', $car);
        } else {
            $this->errorReturn("处理失败");
        }
    }
    /*修改密码*/
    public function updatePassword()
    {
        $_validate = array(
            array('carId', '用户ID必须填写'),
            array('password', '旧的登陆密码必须填写'),
            array('password1', '新的登陆密码必须填写'),
            array('password2', '确认密码必须填写')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }

        $data = $this->getData();
        if ($data['password1'] != $data['password2']) {
            $this->errorReturn("密码输入不一致");
        }
        if (!preg_match('/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,15}$/is', $data['password1'])) {
            $this->errorReturn("密码长度在6~15之间，只能包含字符、数字和下划线");
        }
        $dbCar= M('Car');
        $car = $dbCar->where(array('id' => $data['carId']))->find();
        $pass = password($data['password'], $car['encrypt']);
        if ($car['password'] != $pass) {
            $this->errorReturn("登陆密码错误");
        } else {
            $newpass = password($data['password1']);
            $status = $dbCar->where(array('id' => $data['carId']))->save(array('password' => $newpass['password'],'encrypt'=>$newpass['encrypt']));
            $status ? $this->successReturn('修改登陆密码成功') : $this->errorReturn('修改登陆密码失败');
        }
    }

    public function online(){
        $_validate = array(
            array('carid', '用户不存在')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        if ($data['longitude'] > 0 && $data['latitude'] > 0) {
            $cardb = M('Car');
            $cardb->where('id=' . $data['carid'])->save(array('longitude' => $data['longitude'], 'latitude' => $data['latitude'], 'online' => time()));
        }

        $this->successReturn('操作成功');
    }

    public function myStatus()
    {
        $_validate = array(
            array('mobile', '手机号必须填写')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $cardb = M('Car');
        $car = $cardb->field("id,work_status,carno,load")->where('mobile=\'' . $data['mobile'].'\'')->find();
        if(empty($car)){
            $this->errorReturn('查询出错,请稍后再试...');
        }
        //执行中的任务
        $taskdb = M('Task');
        $task = $taskdb->table("__TASK__ t")->field('t.id,t.site_id,t.task_from,t.task_from_longitude,t.task_from_mobile,t.task_from_latitude,t.task_end,
        t.task_end_longitude,t.task_end_latitude,t.mobile as task_end_mobile,t.car_id,t.status,t.step,t.car_load,t.total_load,t.load_site,
        t.public_time,rd.point,rd.distance,rd.duration')
            ->join("LEFT JOIN __ROADS_DETAIL__ as rd on t.roads_id=rd.roads_id")->where('t.car_id='.$car['id'].' and (t.status=2 or (t.status=1 and t.public_time>'.(time()-180).'))')->find();
        if(empty($task)){
            $task= '';
        }
        $dbMobile = D('Mobile');//获取一键拨号所有电话
        $mobileList = $dbMobile->limit(0,10)->select();
        $car_load_def = C('CAR_LOAD');
        $serviceMobile = C('SERVER_MOBILE');
        $car['mobiles'] = $mobileList;
        $car['service_mobile'] = $serviceMobile;
        $car['car_load_def'] = $car_load_def;
//        Log::write("car_load_def：" . $car_load_def);
        $this->successReturn('获取状态成功', array('status' => $car,'task'=>$task));

    }

    public function changeStatus()
    {
        $_validate = array(
            array('mobile', '手机号必须填写'),
            array('status', '手机号必须填写')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $cardb = M('Car');
        $cardb->where('mobile=' . $data['mobile'])->save(array('work_status' => $data['status']));
        $this->successReturn('操作成功', array('work_status' => $data['status']));
    }

    public function changeCarNo()
    {
        $_validate = array(
            array('carId', '用户ID必须填写'),
            array('carNo', '车号必须填写'),
            array('carLoad', '载重必须填写')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $cardb = M('Car');
        $cardb->where('id=' . $data['carId'])->save(array('carno' => $data['carNo'],'load' => $data['carLoad']));
        $this->successReturn('操作成功', array('carNo' => $data['carNo']));
    }

    public function syncPushinfo(){
        $_validate = array(
            array('mobile', '手机号必须填写'),
            array('userId', '初始化错误'),
            array('channelId', '初始化错误')
        );
        if (!$this->validate($_validate)) {
            $this->errorReturn($this->getError());
        }
        $data = $this->getData();
        $car_db = D('Car');
        $car_db->where(array('mobile' => $data['mobile']))->save(array('user_id'=>$data['userId'],'channel_id'=>$data['channelId']));
        $this->successReturn('操作成功', array('user_id'=>$data['userId'],'channel_id'=>$data['channelId']));
    }
    
    /*用户打卡每个月1号*/
    public  function clock_time(){
        $car_id=I('get.car_id');
        $time=date('d');
        if($car_id){
            $state=M('car')->where(['id'=>$car_id])->save(['clock_time'=>date('Y-m-d H:i:s')]);
            if($state){
                $this->successReturn(array('code'=>200,'msg'=>'打卡成功'));
            }else{
                $this->errorReturn(array('code'=>203,'msg'=>'打卡失败'));
            }
        }else{
            $this->errorReturn(array('code'=>203,'msg'=>'打卡失败'));
        }
    }
}
